/*
* Copyright 2017 Baiyyy
* @(#)ApiLoginController.java
*/

package com.byyy.ccts.modules.api.controller;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.common.controller.BaseApiController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.modules.api.convert.CangkuConvert;
import com.byyy.ccts.modules.api.convert.HuozConvert;
import com.byyy.ccts.modules.api.convert.PsZcdConvert;
import com.byyy.ccts.modules.api.convert.YezConvert;
import com.byyy.ccts.modules.api.entity.WarehouseVo;
import com.byyy.ccts.modules.api.entity.CompanyVo;
import com.byyy.ccts.modules.api.entity.OwnerVo;
import com.byyy.ccts.modules.api.enums.InterfaceTypeEnum;
import com.byyy.ccts.modules.sys.entity.Cangku;
import com.byyy.ccts.modules.sys.entity.Huoz;
import com.byyy.ccts.modules.sys.entity.Yez;
import com.byyy.ccts.modules.sys.service.ICangkuService;
import com.byyy.ccts.modules.sys.service.IHuozService;
import com.byyy.ccts.modules.sys.service.IYezService;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
* Created by DELL on 2019-8-20.
*/
@Controller
@RequestMapping("/api/base")
public class ApiBaseController extends BaseApiController {

    @Autowired
    private ICangkuService cangkuService;

    @Autowired
    private IYezService yezService;

    @Autowired
    private IHuozService huozService;

    /**
     * 仓库接口
     * @param warehouseVo
     * @return
     */
    @RequestMapping(value = "/warehouse", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson warehouse(@RequestBody @Valid WarehouseVo warehouseVo) {
        AjaxJson ajaxJson = new AjaxJson();
        Cangku cangku = CangkuConvert.INSTANCE.warehouseToCangku(warehouseVo);
        if (InterfaceTypeEnum.删除.getValue().equals(warehouseVo.getFlag())) {
            this.cangkuService.deleteById(cangku.getCangku());
        }
        else {
            this.cangkuService.insertOrUpdate(cangku);
        }
        return ajaxJson;
    }

    /**
     * 业主接口
     * @param companyVo
     * @return
     */
    @RequestMapping(value = "/company", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson company(@RequestBody @Valid CompanyVo companyVo) {
        AjaxJson ajaxJson = new AjaxJson();
        Yez yez = YezConvert.INSTANCE.companyToYez(companyVo);
        if (InterfaceTypeEnum.删除.getValue().equals(companyVo.getFlag())) {
            this.yezService.deleteById(yez.getYezId());
        }
        else {
            this.yezService.insertOrUpdate(yez);
        }
        return ajaxJson;
    }

    /**
     * 货主接口
     * @param ownerVo
     * @return
     */
    @RequestMapping(value = "/owner", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson owner(@RequestBody @Valid OwnerVo ownerVo) {
        AjaxJson ajaxJson = new AjaxJson();
        Huoz huoz = HuozConvert.INSTANCE.ownerToHuoz(ownerVo);
        if (InterfaceTypeEnum.删除.getValue().equals(ownerVo.getFlag())) {
            this.huozService.deleteById(huoz.getId());
        }
        else {
            this.huozService.insertOrUpdate(huoz);
        }
        return ajaxJson;
    }

}
